//require(["dojo/dom","dojo/on","dojo/json","dojo/_base/xhr","dojo/domReady!"],
//function(dom,on,json,xhr)
//{
//    var button = dom.byId("submitButton1");
//    function callback_post2(evt)
//    {
//        var _data = {"input1": dom.byId("input1").value,
//                     "input2": dom.byId("input2").value};
//        var xhrArgs = {
//            url: "/data/",
//            postData: json.stringify( _data ),
//            handleAs: "json",
//            headers: { "Content-Type": "application/json"},
//            //data was already parsed from json
//            load: function(data)
//            {
//                dom.byId("log").innerHTML = "Message posted.";
//                dom.byId("output").innerHTML = data.result;
//            },
//            error: function(error)
//            {
//                dom.byId("log").innerHTML = "Message posted.";
//                dom.byId("error").innerHTML = error;
//            }
//        }
//        dom.byId("log").innerHTML = "Message being sent..."
//        var deferred = xhr.post(xhrArgs);
//    };
//    on(button, "click", callback_post2);
//});
require([
	"dojo/on",
	"dojo/dom",
	"dojo/dom-construct",
	"dojo/query",
	"dojo/_base/Deferred",
	"dojo/store/JsonRest",
	"dojo/store/Memory",
	"dojo/store/Cache",
	"dojo/store/Observable",
	"dojo/Stateful",
	"dojo/json",
	"dojo/mouse",
	"dojo/dom-style",
	"dojo/topic",
	"dojo/domReady!"
	],
function(on, dom, domConstruct, query, Deferred, JsonRest, Memory, Cache, Observable, Stateful, json, mouse,domStyle,topic)
{
    //http://stackoverflow.com/questions/8530364/create-dynamic-pages-from-json-in-dojo
    var results;
    function viewResults(results)
    {
        var container = dom.byId("notices");
        var rows = [];
        // results object provides a forEach method for iteration
        results.forEach(insertRow);
	query(".row").forEach(function(node, index, nodelist){
	     domStyle.set(node, "backgroundColor", "blue");
	});
	topic.subscribe("mouseover",function(msg, id){
	     domStyle.set(dom.byId(id), "backgroundColor", msg);
	});
	query(".row").on(mouse.enter, function(){
	    topic.publish("mouseover","red", this.id);
	});
	query(".row").on(mouse.leave, function(){
	    topic.publish("mouseover","blue", this.id);
	});

        results.observe(function(item, removedIndex, insertedIndex)
        {
            // this will be called any time an item is added, removed, and updates    
            if(removedIndex > -1)
            {
                removeRow(removedIndex);
            }
            if(insertedIndex > -1)
            {
                insertRow(item, insertedIndex);
            }
        }, true); // we can indicate to be notified of object updates as well

        function insertRow(item, i)
        {
            var row = domConstruct.create("div",
            {
                className: "row",
            });
            row.id = item.id;
            function construct(i_class,i_data)
            {
                return domConstruct.create("div",
                {
                    className: i_class,
                    innerHTML: i_data 
                });
            }
            domConstruct.place(construct("span2",item.id), row);
            domConstruct.place(construct("span2",item.name), row);
            domConstruct.place(construct("span2",item.descr), row);
            domConstruct.place(construct("span2",item.shop_name), row);
            domConstruct.place(construct("span2",item.planned_day_of_order), row);
            
            rows.splice(i, 0, container.insertBefore(row, rows[i]));
        }
        function removeRow(i)
        {
            domConstruct.destroy(rows.splice(i, 1)[0]);
        }
    }

    var notice_store = new Memory({});
    var notice_rest	= new JsonRest({target: "/notice/"});
    notice_store = Observable(notice_store);
    var defered = notice_rest.query("");
    defered.then(function(data){
		    dom.byId("log").innerHTML = "Message geted.";
		    dom.byId("output").innerHTML = json.stringify( data );
                    notice_store.setData(data);
                    results = notice_store.query({});
                    viewResults(results);
		});

    function save()
    {
        var _data = {"name": dom.byId("name").value,
                    "descr": dom.byId("descr").value,
                    "shop_name": dom.byId("shop_name").value,
                    "planned_day_of_order": dom.byId("planned_day_of_order").value,};
        var def = notice_rest.add(_data);
        def.then(function(data){
		    dom.byId("log").innerHTML = "Message posted.";
		    dom.byId("output").innerHTML = json.stringify( data );
                    notice_store.put(data[0]);
		});
    }
    on(dom.byId("add_notice"), "click", save);
    // mouse.leave
    var handler1 = {
	id : "handler1",
	mouse_enter : function(evt){
	    domStyle.set(dom.byId(this.id), "backgroundColor", "red");
	},
	mouse_leave : function(evt){
	    domStyle.set(dom.byId(this.id), "backgroundColor", "");
	}
    };
//    query(".row").on(mouse.enter, handler1.mouse_enter );
//    query(".row").on(mouse.leave, handler1.mouse_leave );

})
